var app = getApp();
var util = require('../../utils/util.js');
var DB = require('../../utils/db.js');
var QR = require('../../utils/wxqrcode.js');
var size;

Page({
  data:{
    tabbar:["我的信息","我的校证"],
    tabIndex:0,
    qrImg: '',
    studentID:"",
    name:"",
    // studentID: app.globalData.studentID,
    // name: app.globalData.name,
    src:"",
    show:true,  //用于显示信息隐藏信息
    head:"../../images/head.jpg",
    src:"",
    appear:false,
    taskId:""
  },



  onLoad:function(options){
    this.size = this.getQRCodeSize();
    var student = wx.getStorageSync("student")
    var studentID = student.studentID
    var name = student.name
    var grade = student.grade
    app.globalData.name = student.name
    let SRC = 'http://class.sise.com.cn:7001/sise/module/studentphoto/' + grade + '/' + studentID + '.jpg'
    this.setData({
      studentID: studentID,
      name: name,
      src: SRC
    }) 
  },
  
  
  //上面tabbar
  tabbar:function(e){
    const index = e.currentTarget.dataset.index;
    this.setData({tabIndex:index});
    if(index == 1){
    }
  },

  //获取设备宽高
  getQRCodeSize: function () {
    var size = 0;
    try {
      var res = wx.getSystemInfoSync();
      var scale = res.windowWidth / 750;  
      var width = 300 * scale;
      size = width;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
      size = 150;
    }
    return size;
  },

  //生成二维码
  createQRCode: function (text, size) {
    let that = this
    try {
      // console.log('QRcode: ', text, size)
      let _img = QR.createQrCodeImg(text, {
        size: parseInt(size)
      })
      that.setData({
        qrImg: _img
      })
    } catch (e) {
      console.log(e)
    }
  },


  //获取设备宽高
  getQRCodeSize: function () {
    var size = 0;
    try {
      var res = wx.getSystemInfoSync();
      var scale = res.windowWidth / 750;  
      var width = 300 * scale;
      size = width;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
      size = 150;
    }
    return size;
  },
  buttoninfo:function(){
    var show = this.data.show
    this.setData({show:!show})
  },

  infomation:function(){
    var text=this.data.studentID+"/"+this.data.time;
    var appear = this.data.appear
    this.setData({appear:!appear})
    this.createQRCode(text, this.size)
    wx.cloud.callFunction({
      name:"get_student_openid",
      data:{
        studentID:this.data.studentID
      }
    }).then(res=>{
      if(res.result.length==0){
        wx.cloud.callFunction({
          name:"addopenid",
          data:{
            studentID:this.data.studentID,
          }
        }).then(res=>{
          console.log(res)
        })
      }
    })
    wx.requestSubscribeMessage({
      tmplIds: ['R5pgNdl5ew2oBKdnr3iPLAeWOYUBM9rfUyt9s4qtY1s'],
      success: res => {
        console.log('success',res)
      },
      fail: err => {
        console.log('err', err)
      }
    })
  },

  onPullDownRefresh: function () {
    var text = this.data.studentID + "/" + this.data.time;
    wx.showNavigationBarLoading()
    this.onShow()
    this.createQRCode(text, this.size)
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    }, 1000);
  },

  onShow: function (e) {
    //获取时间戳
    this.time = new Date();
    //时间戳转换
    var date = util.formatTime(this.time);
    this.setData({ time: date })
  },
  tomap:function(){
    wx.showToast({
      title: '努力获取中',
      icon: 'loading',
      mask: true
    })
    wx.getImageInfo({     //获取图片信息
      src: 'cloud://epms-aocnh.6570-epms-aocnh-1300754993/school.jpg',
      success(res){
        console.log(res)
        wx.previewImage({    //预览图片
          urls: [res.path],
        })
      }
    })
  }

})